<?php
namespace Backend\Models\Member;

use Backend\Models\Base;

class MemberList extends Base
{
    public function initialize()
    {
        parent::initialize();
    }

    public function getSource()
    {
        return 'daishu_merchant_member';
    }

    public function add($entity)
    {
        try {
            $this->db()->begin();
            $memEntity = [
                'name'      => $entity['name'],
                'account'   => $entity['account'],
                'password'  => $entity['password'],
                'phone'     => $entity['phone'],
                'sex'       => $entity['sex'],
                'status'    => $entity['status'],
                'age'       => $entity['age'],
                'create_at' => $entity['create_at']
            ];
            $this->db()->insertAsDict(self::getSource(), $memEntity);
            $uid = $this->db()->lastInsertId();
            if ($uid == false) {
                $this->db()->rollback();
                return false;
            }
            unset($memEntity);
            $infoEntity = [
                'uid'       => $uid,
                'role_id'   => $entity['role_id'],
                'work_id'   => $entity['work_id'],
                'create_at' => $entity['create_at'],
                'work_time' => $entity['work_time'],
                'skill'     => $entity['skill']
            ];
            $sidArr     = $entity['sid'];
            unset($entity);
            foreach ($sidArr as $k => $v) {
                $data = [
                    'uid'       => $infoEntity['uid'],
                    'role_id'   => $infoEntity['role_id'],
                    'work_id'   => $infoEntity['work_id'],
                    'create_at' => $infoEntity['create_at'],
                    'work_time' => $infoEntity['work_time'],
                    'skill'     => $infoEntity['skill'],
                    'sid'       => $v
                ];
                $ret  = $this->db()->insertAsDict('daishu_merchant_member_info', $data);
                if ($ret == false) {
                    $this->db()->rollback();
                    return false;
                }
            }
            $this->db()->commit();
            return true;
        } catch (\PDOException $ex) {
            $this->errorLog('INSERT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function getByAccount($account)
    {
        try {
            $user = $this->db()->query('SELECT * FROM `daishu_merchant_member` WHERE `account`=:account LIMIT 1', [
                'account' => $account
            ])->fetch();
            if ($user !== false && is_array($user) && count($user) > 0) {
                return $user;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function getStoreName()
    {
        try {
            $query  = 'SELECT `id`,`name` FROM `daishu_store`';
            $result = $this->db()->query($query)->fetchAll();
            if ($result !== false && is_array($result) && count($result) > 0) {
                return $result;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function getWorkName()
    {
        try {
            $query  = 'SELECT `wid`,`name` FROM `daishu_work_type`';
            $result = $this->db()->query($query)->fetchAll();
            if ($result !== false && is_array($result) && count($result) > 0) {
                return $result;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function getRoleName()
    {
        try {
            $query  = 'SELECT `id`,`name` FROM `merchant_member_role` WHERE `status`=1';
            $result = $this->db()->query($query)->fetchAll();
            if ($result !== false && is_array($result) && count($result) > 0) {
                return $result;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function gets($limit, $offset)
    {
        $query = "SELECT m.uid,m.name,i.`role_id`,group_concat(concat_ws(',',i.`sid`) order by m.`uid` ) as `sid`,i.work_id,m.account,m.`head_url`,m.status,m.`create_at` FROM `daishu_merchant_member` as `m`
                  LEFT JOIN  `daishu_merchant_member_info` AS `i` ON m.`uid`=i.`uid` GROUP BY m.`uid`  ORDER BY  m.`uid` DESC  LIMIT " . $limit . '  OFFSET  ' . $offset;
        try {
            $ret = $this->db()->query($query)->fetchAll();
            if ($ret !== false && is_array($ret) && count($ret) > 0) {
                $types = $this->getWorkName();
                $out   = [];
                foreach ($types as $k => $v) {
                    $out[$v['wid']] = $v['name'];
                }
                $roles = $this->getRoleName();
                $data  = [];
                foreach ($roles as $k => $v) {
                    $data[$v['id']] = $v['name'];
                }
                $stores = $this->getStoreName();
                $arr    = [];
                foreach ($stores as $k => $v) {
                    $arr[$v['id']] = $v['name'];
                }
                foreach ($ret as $k => $v) {
                    if (array_key_exists($v['work_id'], $out)) {
                        $ret[$k]['work_name'] = $out[$v['work_id']];
                    }
                    if (array_key_exists($v['role_id'], $data)) {
                        $ret[$k]['role_name'] = $data[$v['role_id']];
                    }
                    if ($v['sid'] != '') {
                        $sids      = explode(',', $v['sid']);
                        $shop_name = '';
                        if (is_array($sids) && count($sids) > 0) {
                            foreach ($sids as $_v) {
                                if (array_key_exists($_v, $arr)) {
                                    $shop_name .= $arr[$_v] . "<br>";
                                }
                            }
                            $ret[$k]['shop_name'] = rtrim($shop_name, ',');
                        }
                    }
                }
                return $ret;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function getCount()
    {
        $query = 'SELECT COUNT(0) AS `cnt` FROM ' . self::getSource();
        try {
            $ret = $this->db()->query($query)->fetch();
            if ($ret !== false && is_array($ret) && count($ret) > 0) {
                return $ret['cnt'];
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

}
